/**
 *
 * Copyright (c) 2011 TLOSS. All rights reserved.
 * Created on Apr 22, 2011
 *
 */
package com.tloss.database.db2obj.classpath;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLClassLoader;
import java.util.Properties;

import org.apache.tools.ant.Project;
import org.apache.tools.ant.ProjectHelper;
import org.apache.velocity.Template;
import org.apache.velocity.VelocityContext;
import org.apache.velocity.app.VelocityEngine;

/**
 * @author Tran Tung
 * 
 */
public class ClassLoaderUtil {
	public void createAntFile(String outPutFile, Configuration configuration)
			throws IOException {
		Properties p = new Properties();
		p.setProperty("resource.loader", "file");
		p.setProperty("file.resource.loader.class",
				"org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader");
		p.setProperty("input.encoding", "UTF-8");
		p.setProperty("output.encoding", "UTF-8");
		VelocityEngine ve = new VelocityEngine();
		ve.init(p);
		Template t = ve
				.getTemplate("/com/tloss/database/db2obj/classpath/velocity/builld.xml.vm");
		VelocityContext context = new VelocityContext();
		context.put("projectName", configuration.getProjectName());
		context.put("sources", configuration.getSources());
		context.put("libraries", configuration.getLibraries());
		context.put("compilerSource", configuration.getCompilerSource());
		context.put("compilerTarget", configuration.getCompilerTarget());
		FileWriter writer = new FileWriter(outPutFile);
		t.merge(context, writer);
		writer.flush();
		writer.close();

	}

	public void runCompile(String outPutFile) {
		File buildFile = new File(outPutFile);
		Project p = new Project();
		p.setUserProperty("ant.file", buildFile.getAbsolutePath());
		p.init();
		ProjectHelper helper = ProjectHelper.getProjectHelper();
		p.addReference("ant.projectHelper", helper);
		helper.parse(p, buildFile);
		p.executeTarget(p.getDefaultTarget());
	}

	public java.lang.ClassLoader getClassLoader(java.lang.ClassLoader current,
			String jar) throws Exception {
		File file = new File(jar);
		URLClassLoader loader;
		try {
			loader = new URLClassLoader(new URL[] { file.toURI().toURL() },
					current);
		} catch (MalformedURLException e) {
			throw new Exception(e);
		}
		return loader;
	}

	public static void main(String[] args) throws Exception {
		ConfigurationImpl configurationImpl = new ConfigurationImpl();
		configurationImpl.readConfig(ClassLoaderUtil.class
				.getResourceAsStream("/project.xml"));
		ClassLoaderUtil classLoader = new ClassLoaderUtil();
		classLoader.createAntFile("build.xml", configurationImpl);
		classLoader.runCompile("build.xml");
		java.lang.ClassLoader current = Thread.currentThread()
				.getContextClassLoader();
		java.lang.ClassLoader loader = classLoader.getClassLoader(current,
				"build/" + configurationImpl.getProjectName() + ".jar");
		System.out.println(loader);
	}
}
